from django.db import models

# Create your models here.
from user.models import Department, Doctor, Medicine
from utils.common_basemodel import BaseModels, ModelRedMethod
from patient.models import HospitalInfo


# 预约表
class RegisterInfo(BaseModels, ModelRedMethod):
    subscribe_time = models.DateTimeField(verbose_name='预约时间')
    department = models.ForeignKey(to=Department, db_constraint=False, default=0, null=True, on_delete=models.CASCADE,
                                   verbose_name='预约科室')
    illness_desc = models.TextField(verbose_name='病情描述')
    doctor = models.ForeignKey(to=Doctor, on_delete=models.CASCADE, verbose_name='预约医生')

    class Meta:
        db_table = 'hospital_registered'
        verbose_name = '挂号信息'
        verbose_name_plural = verbose_name


    def __str__(self):
        return self.name

    def doctor_name(self):
        return self.doctor.name


class DetailedList(models.Model):
    type = (
        (1, '未缴费'),
        (2, '缴费中'),
        (3, '已缴费'),
    )
    patient_name = models.CharField(max_length=32, verbose_name='病人姓名')
    doctor_name = models.CharField(max_length=32, verbose_name='开清单医生')
    mobile = models.CharField(max_length=32, verbose_name='联系电话')
    total_price = models.CharField(max_length=32, verbose_name='清单价格')
    careful_note = models.TextField(verbose_name='注意事项')
    status = models.SmallIntegerField(choices=type, default=1, verbose_name='清单状态')
    medicine = models.ManyToManyField(to=Medicine)

    def status_name(self):
        return self.get_status_display()

    # 计算药品价格和住房价格

    def price(self):
        try:
            user = HospitalInfo.objects.get(name=self.patient_name,mobile=self.mobile)
            final_price = str(int(self.total_price) + (int(user.time_length) * 50))
            return final_price
        except Exception as e:
            return self.total_price

    def medicine_list(self):  # 列表里面套字典
        medicine_info_list = []
        for medicine in self.medicine.all():
            medicine_info_list.append(
                {'id':medicine.id,'medicine_name': medicine.name, 'medicine_price': medicine.price,
                 'medicine_category': medicine.category})
        return medicine_info_list
